package cn.agiledata.bank.information.action;

import java.util.Date;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.common.dao.SysTemplate;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.ImageUtil;
import cn.agiledata.bank.common.util.PageBean;
import cn.agiledata.bank.common.util.StringUtil;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.common.util.WebUtil;
import cn.agiledata.bank.hibernateBean.Information;
import cn.agiledata.bank.hibernateBean.SysParameter;
import cn.agiledata.bank.information.dao.FinanceListDao;
import cn.agiledata.bank.information.dao.InfoList;
import cn.agiledata.bank.information.form.FinanceForm;
import cn.agiledata.bank.information.form.InformationBean;
import cn.agiledata.bank.information.form.InformationForm;
import cn.agiledata.bank.information.service.IInformationService;
import cn.agiledata.bank.scores.service.IScoresService;
import cn.agiledata.bank.scores.service.ScoresFactory;
import com.vnv.infrastructure.action.IPBBaseAction;
import com.vnv.infrastructure.exception.IPBException;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class InformationAction extends IPBBaseAction {
	 private static final String CONTENT_TYPE = "image/jpeg";
	public int hasAccessPermission(int accessLevel, HttpServletRequest request)
			throws IPBException {
		return 1;
	}

	private IInformationService getInfoService() {
		return (IInformationService) IPBAppFactory.getObj("informationService");
	}

	public ActionForward toInformationMain(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws IPBException, Exception {
		IInformationService service = getInfoService();
		InformationForm iform = (InformationForm) actionForm;
		service.setForm(iform);
		WebUtil.setSessionString(request, "modual", "space");
		
		return mapping.findForward("access");
	}
	
	/**
	 * 查询取得充值实际金额
	 * 
	 * @param request
	 */
	private void getChargetRealAmt(HttpServletRequest request) {
		SysTemplate sysTemplate = (SysTemplate) IPBAppFactory
				.getObj("sysTemplate");
		SysParameter spCharge50 = sysTemplate.getParamByType(Constant.SYS_PARAM_MOBILE_CHARGE_50);
		SysParameter spCharge100 = sysTemplate.getParamByType(Constant.SYS_PARAM_MOBILE_CHARGE_100);
		long accountId = WeatherUtil.getAccountIdFromSession(request);
		if (accountId != 0) {
			spCharge50 = sysTemplate.getParamByType(Constant.SYS_PARAM_MOBILE_CHARGE_UU_50);
			spCharge100 = sysTemplate.getParamByType(Constant.SYS_PARAM_MOBILE_CHARGE_UU_100);
		}
		request.setAttribute("charge50Value", spCharge50.getParmValue());
		request.setAttribute("charge100Value", spCharge100.getParmValue());
	}
	
	public ActionForward toInformationList(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws IPBException, Exception {
		String kind = request.getParameter("kind");
		InformationBean bean = new InformationBean();
		bean.setKind(kind);
		InformationForm iform = (InformationForm) actionForm;
		if (StringUtil.isStrEmpty(iform.getStartPage())) {
			bean.setStartPage("1");
		} else {
			bean.setStartPage(iform.getStartPage());
		}
		bean.setNow(new Date());
		request.setAttribute("kind", kind);
		InfoList list = new InfoList(bean);
		PageBean pageBean = list.getAccountListPageBean();
		request.setAttribute("list", pageBean);
		return mapping.findForward("access");
	}

	public ActionForward toInformationDetail(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws IPBException, Exception {
		String id = request.getParameter("id");
		IInformationService service = getInfoService();
		Information information = service.getInformationById(id);
		request.setAttribute("info", information);
		if (information.getKind().equals(Constant.INFORMATION_NOTICE)) {
			return mapping.findForward("finance");
		} else {
			return mapping.findForward("others");
		}
	}

	public ActionForward toFinance(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws IPBException, Exception {
		IInformationService service = getInfoService();
		FinanceForm fform = (FinanceForm) actionForm;
		service.setFinanceForm(fform);
		return mapping.findForward("access");
	}

	public ActionForward toFinanceList(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws IPBException, Exception {
		String type = request.getParameter("type");
		InformationBean bean = new InformationBean();
		if(type.equals("1")){
			bean.setType1(Constant.FINANCE_ROLED);
			bean.setType2(Constant.FINANCE_DEVELOPE);
			request.setAttribute("type1", Constant.FINANCE_ROLED);
			request.setAttribute("type2", Constant.FINANCE_DEVELOPE);
		}
		if(type.equals("2")){
			bean.setType1(Constant.FINANCE_FEATURE);
			bean.setType2(Constant.FINANCE_PERSPECTIVE);
			request.setAttribute("type1", Constant.FINANCE_FEATURE);
		    request.setAttribute("type2", Constant.FINANCE_PERSPECTIVE);
		}
		FinanceForm iform = (FinanceForm) actionForm;
		if (StringUtil.isStrEmpty(iform.getStartPage())) {
			bean.setStartPage("1");
		} else {
			bean.setStartPage(iform.getStartPage());
		}
		bean.setNow(new Date());
		FinanceListDao list = new FinanceListDao(bean);
		PageBean pageBean = list.getAccountListPageBean();
		request.setAttribute("typelist", pageBean);
		request.setAttribute("type", type);
		return mapping.findForward("access");
	}
	
	public ActionForward toMySpace(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws IPBException, Exception {
		WebUtil.setSessionString(request, "modual", "space");
		return mapping.findForward("access");
	}
	
	public ActionForward toMySpaceCorp(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws IPBException, Exception {
		WebUtil.setSessionString(request, "modual", "space");
		return mapping.findForward("access");
	}

	public ActionForward toPersonalPay(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws IPBException, Exception {
		WebUtil.setSessionString(request, "modual", "space");
		return mapping.findForward("access");
	}

	public ActionForward toCoreporationPay(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws IPBException, Exception {
		WebUtil.setSessionString(request, "modual", "space");
		return mapping.findForward("access");
	}

	public ActionForward toFinanceIndex(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws IPBException, Exception {
		IInformationService service = getInfoService();
		List list = service.getInformationPick("Y");
		request.setAttribute("list", list);
		return mapping.findForward("access");
	}
	public ActionForward showInfoPic(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws IPBException, Exception {
    	//InformationForm iform = (InformationForm)actionForm;
    	 IInformationService service = (IInformationService) IPBAppFactory.getObj("informationService");
        String id = request.getParameter("picId");
        Information information = service.getInformationById(id);
        response.setContentType(CONTENT_TYPE);
        ServletOutputStream out = response.getOutputStream();
        String compressSizeValue = request.getParameter("compressSize");
        double compressSize;
        try {
            compressSize = Double.parseDouble(compressSizeValue);
        } catch (Exception e) {
            compressSize = 0;
        }
        String defaultImageUri = WebUtil.getRoot(request) + "/images/noPhoto.png";
        ImageUtil.blobImageToOutputStream(information.getPics(), out,
                compressSize, defaultImageUri);
        return null;
    }
	private IScoresService iss = ScoresFactory.getScoresService();
	
//	查询当前积分规则
	public ActionForward getScoresRule(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse reponse) throws IPBException, Exception{
		List common = iss.getCurScoresRule(new Long(Constant.CUSTOMER_GRADING_A));
		List advance = iss.getCurScoresRule(new Long(Constant.CUSTOMER_GRADING_B));
		List vip = iss.getCurScoresRule(new Long(Constant.CUSTOMER_GRADING_C));
		request.getSession().setAttribute("common", common);
		request.getSession().setAttribute("advance", advance);
		request.getSession().setAttribute("vip", vip);
//		String value = iss.getSysParaByName(Constant.SYS_PARAM_MONTH_SCORE_LIMITE).getValue();
//		request.setAttribute("monthLimit", value);
		return mapping.findForward("success");
	}
	
	public ActionForward scoresRule(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse reponse) throws IPBException, Exception{
		return mapping.findForward("success");
	}
}
